package cn.usain.controller;

import cn.usain.entity.Leader;
import cn.usain.entity.vo.UpdatePasswordVO;
import cn.usain.service.LeaderService;
import cn.usain.service.PersonalService;
import com.jiyun.Result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@RestController
@RequestMapping("/leaders")
public class LeadersController {

    @Autowired
    public  LeaderService leaderService;

    @Autowired
    private PersonalService personalService;

    @PostMapping("/add")
    public Result applyForLeader(@RequestBody Leader leader) {
        leaderService.addGroupLeader(leader);
        return Result.ok();
    }

    @GetMapping("/findList")
    public Result findList() {
        return Result.ok(personalService.findList());
    }



    // 定义图片保存的基础目录，可根据实际需求调整路径，这里假设保存到项目根目录下的uploads文件夹
    private static final String UPLOAD_DIR = "D://liuchnegtu";

    @PostMapping("/api/upload")
    public ResponseEntity<String> uploadFile(MultipartFile file) {
        // 检查文件是否为空
        if (file.isEmpty()) {
            return new ResponseEntity<>("请选择要上传的图片文件", HttpStatus.BAD_REQUEST);
        }

        // 获取原始文件名
        String originalFileName = file.getOriginalFilename();
        // 获取文件后缀名
        String fileExtension = getFileExtension(originalFileName);
        // 定义允许上传的图片文件类型，可根据实际情况扩展或修改
        String[] allowedExtensions = {"jpg", "jpeg", "png", "gif"};
        boolean isAllowedExtension = false;
        for (String ext : allowedExtensions) {
            if (ext.equalsIgnoreCase(fileExtension)) {
                isAllowedExtension = true;
                break;
            }
        }
        if (!isAllowedExtension) {
            return new ResponseEntity<>("不允许上传此类型的图片文件", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
        }

        try {
            // 生成唯一的文件名，防止文件名重复
            String uniqueFileName = UUID.randomUUID().toString() + "." + fileExtension;
            // 创建保存文件的完整路径
            Path uploadPath = Paths.get(UPLOAD_DIR, uniqueFileName);
            Files.createDirectories(uploadPath.getParent());
            // 将文件保存到指定路径
            file.transferTo(uploadPath.toFile());
            return new ResponseEntity<>("图片上传成功，图片访问地址：" + "/liuchnegtu/" + uniqueFileName, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>("图片上传失败，请稍后再试", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/api/applyLeader")
    public ResponseEntity<String> applyLeader(@RequestBody Leader leader) {
        // 在这里可以添加将申请数据保存到数据库等实际业务逻辑，比如调用Service层方法来持久化数据
        System.out.println("接收到申请团长数据：" + leader);
        return new ResponseEntity<>("申请已成功接收，正在审核中", HttpStatus.OK);
    }

    private String getFileExtension(String fileName) {
        if (fileName.lastIndexOf(".")!= -1 && fileName.lastIndexOf(".")!= 0) {
            return fileName.substring(fileName.lastIndexOf(".") + 1);
        }
        return "";
    }




}
